package com.oa.srms.apps.business.staff.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.business.organization.IPostInfoService;
import com.oa.srms.apps.business.staff.IStaffInfoService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.staff.IStaffInfoDao;
import com.oa.srms.apps.entity.staff.StaffInfoValue;

public class StaffInfoServiceImpl implements IStaffInfoService {
	
	@Autowired
	private StaffInfoValue staffInfo;
	@Autowired
	private IPostInfoService postInfoService;
	@Autowired
	private IStaffInfoDao infoDao;

	@Override
	public StaffInfoValue verifyStaffInfo(String staffNo, String staffPwd)
			throws Exception {
		if (staffNo != null)
			staffInfo.setStaffNo(staffNo);
		if (staffPwd != null)
			staffInfo.setStaffPwd(staffPwd);
		for (StaffInfoValue siv : infoDao.verifyStaffInfo(staffInfo)) {
			if (siv != null)
				return siv;
		}
		return null;
	}
	
	@Override
	public Boolean verifyStaffNo(String staffNo) throws Exception{
		if (staffNo != null)
			staffInfo.setStaffNo(staffNo);
		if (infoDao.verifyStaffInfo(staffInfo).get(0) != null) {
			return true;
		}
		return false;
	}

	@Override
	public Integer add(String staffNo, String staffName, String staffPwd,
			Integer postId) throws Exception{
		staffInfo.setStaffNo(staffNo);
		staffInfo.setStaffName(staffName);
		staffInfo.setStaffPwd(staffPwd);
		staffInfo.setPostInfo(postInfoService.queryByPostId(postId));
		staffInfo.setStates(IDEDStates.ENABLE);
		
		infoDao.add(staffInfo);
		
		return staffInfo.getStaffId();
	}

	@Override
	public void modify(Integer staffId, String staffName,
			String staffPwd, Integer postId) throws Exception{
		staffInfo = infoDao.queryByStaffId(staffId);
		if(staffPwd != null){
			staffInfo.setStaffPwd(staffPwd);
		}else{
			staffInfo.setStaffName(staffName);
			staffInfo.setPostInfo(postInfoService.queryByPostId(postId));
		}
		
		infoDao.edit(staffInfo);
		
	}

	@Override
	public void delete(Integer staffId) throws Exception{
		staffInfo = infoDao.queryByStaffId(staffId);
		
		staffInfo.setStates(IDEDStates.DELETE);
		
		infoDao.edit(staffInfo);
		
	}

	@Override
	public StaffInfoValue queryByStaffId(Integer staffId) throws Exception{
		return infoDao.queryByStaffId(staffId);
	}

	@Override
	public List<StaffInfoValue> queryStaffInfo(String staffName,
			Integer postId, Integer states, Integer rows, Integer pageNo) throws Exception{
		if(staffName != null)
		staffInfo.setStaffName(staffName);
		if(postId != null)
		staffInfo.setPostInfo(postInfoService.queryByPostId(postId));
		if(states != null)
		staffInfo.setStates(states);
		return infoDao.queryStaffInfo(staffInfo, rows, pageNo);
	}

	@Override
	public Integer getTotalPageNumber(String staffName, Integer postId,
			Integer states, Integer rows) throws Exception {
		if(staffName != null)
		staffInfo.setStaffName(staffName);
		if(postId != null)
		staffInfo.setPostInfo(postInfoService.queryByPostId(postId));
		if(states != null)
		staffInfo.setStates(states);
		return infoDao.getTotalPageNumber(staffInfo, rows);
	}

	@Override
	public Integer getTotatNumber() {
		return infoDao.getTotalNumber();
	}

	@Override
	public void toEnable(Integer staffId) throws Exception {
		staffInfo = infoDao.queryByStaffId(staffId);
		
		staffInfo.setStates(IDEDStates.ENABLE);
		
		infoDao.edit(staffInfo);
		
	}

	@Override
	public void toDisable(Integer staffId) throws Exception {
		staffInfo = infoDao.queryByStaffId(staffId);
		
		staffInfo.setStates(IDEDStates.DISABLE);
		
		infoDao.edit(staffInfo);
		
	}

}
